package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductCon;
import cn.wolfcode.trip.base.mapper.ProductConMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述： 商品业务层实现类
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/31 17:19
 * 总结:
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductConMapper conMapper;

    @Override
    public void saveOrUpdate(Product product) {
        if (product.getId() != null) {
            // 更新商品详情
            conMapper.updateByPrimaryKey(product.getContent());
            productMapper.updateByPrimaryKey(product);
        } else {
            // 添加时间
            product.setAddTime(new Date());
            // 设置商品详情
            conMapper.insert(product.getContent());
            productMapper.insert(product);
        }
    }

    @Override
    public PageInfo<Product> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Product> list = productMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Product> listAll() {
        return productMapper.selectAll();
    }

    @Override
    public ProductCon getContentById(Long id) {
        return conMapper.selectByPrimaryKey(id);
    }

    @Override
    public void delete(Long id) {
        // 查询指定商品的内容id
        Long conId = productMapper.selectConIdById(id);
        // 删除内容
        conMapper.deleteByPrimaryKey(conId);
        // 删除商品
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Product> queryForApp(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Product> list = productMapper.selectForAppList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public int updateSalesNumAndStoreNumById(Long id) {
        return productMapper.updateSalesNumAndStoreNumById(id);
    }

}
